﻿Namespace GUI

    Friend Class MirScrollBar
        Implements Windows.GUIObj

        Dim mWnd As MirWindow
        Dim mName As String

        Public mPosX As Integer, mPosY As Integer
        Dim mWidth As Integer, mHeight As Integer

        Dim mMaxVal As Integer

        Public Property Maximum() As Integer
            Get
                Return mMaxVal
            End Get
            Set(ByVal value As Integer)
                mMaxVal = value
                CalcButtonRect()
                If mVal >= mMaxVal Then
                    mVal = mMaxVal - 1
                    RaiseEvent Changed()
                End If
            End Set
        End Property

        Dim mButtonLib As EvoLib.Protocol.ImgLibs
        Dim mButtonImage As Integer

        Dim mRectBar As Rectangle
        Dim mRectButton As Rectangle

        Private mVal As Integer

        Public ReadOnly Property Value() As Integer
            Get
                Return mVal
            End Get
        End Property

        Private mVisible As Boolean

        Public Property Visible() As Boolean
            Get
                Return mVisible
            End Get
            Set(ByVal value As Boolean)
                mVisible = value
            End Set
        End Property

        Public Event Changed()

        Public Sub New(ByRef wnd As MirWindow, ByRef name As String, ByRef x As Integer, ByRef y As Integer, _
                       ByRef width As Integer, ByRef height As Integer, ByRef maxval As Integer, ByRef buttonlib As EvoLib.Protocol.ImgLibs, ByRef buttonimg As Integer)

            mWnd = wnd
            mName = name
            mPosX = x
            mPosY = y
            mWidth = width
            mHeight = height
            mMaxVal = maxval
            mButtonLib = buttonlib
            mButtonImage = buttonimg

            mVisible = True

            CalcClientRect()

            mWnd.AddInterfaceItem(Me)

        End Sub

        Private Sub CalcClientRect()

            mRectBar = New Rectangle(mPosX, mPosY, mWidth, mHeight)
            CalcButtonRect()
        End Sub

        Private Sub CalcButtonRect()

            With Libs.GetLib(mButtonLib).Image(mButtonImage).Header

                Dim w As Integer = .Width
                Dim h As Integer = .Height
                Dim x As Integer = (mPosX + (mWidth / 2)) - (w / 2)
                Dim y As Integer
                If mMaxVal > 1 Then
                    y = mPosY + (((mHeight - h) / (mMaxVal - 1)) * mVal)
                Else
                    y = mPosY
                End If
                mRectButton = New Rectangle(x, y, w, h)

            End With

        End Sub

        Public Function Contains(ByRef x As Integer, ByRef y As Integer) As Boolean Implements Windows.mWindows.GUIObj.Contains
            If mRectBar.Contains(x, y) Then Return True
            If mRectButton.Contains(x, y) Then Return True
        End Function

        Public Sub Destroy() Implements Windows.mWindows.GUIObj.Destroy
            mWnd.RemoveInterfaceItem(Me)
        End Sub

        Public Sub Draw(ByRef mode As Globals.mGlobalVars.DrawingMode, ByRef x As Integer, ByRef y As Integer, ByRef z As Single) Implements Windows.mWindows.GUIObj.Draw

            If Not mVisible Then Exit Sub

            If mode = Globals.DrawingMode.Normal Then

                Libs.GetLib(mButtonLib).Image(mButtonImage).Draw(x + mRectButton.Left, y + mRectButton.Top, z)

            End If

        End Sub

        Public Sub Focus() Implements Windows.mWindows.GUIObj.Focus

        End Sub

        Public ReadOnly Property GetsFocus() As Boolean Implements Windows.mWindows.GUIObj.GetsFocus
            Get

            End Get
        End Property

        Public Sub KeyboardInput(ByRef type As Keyboard.mKeyboardVars.KeyboardInputType, ByRef e As System.Windows.Forms.KeyEventArgs) Implements Windows.mWindows.GUIObj.KeyboardInput

        End Sub

        Public Sub KeyboardInput(ByRef type As Keyboard.mKeyboardVars.KeyboardInputType, ByRef e As System.Windows.Forms.KeyPressEventArgs) Implements Windows.mWindows.GUIObj.KeyboardInput

        End Sub

        Public Sub MouseInput(ByRef type As Mouse.mMouseVars.MouseInputType, ByRef e As System.Windows.Forms.MouseEventArgs) Implements Windows.mWindows.GUIObj.MouseInput

            If Not mVisible Then Exit Sub

            If mRectButton.Contains(e.X, e.Y) Then

                If type = Mouse.MouseInputType.MouseDown Then

                    Mouse.MovingObj = Me

                    Windows.FocusWindow(mWnd)
                    Windows.FocusInterface(Me)

                End If

            End If

            If type = Mouse.MouseInputType.MouseMove And Mouse.bIsDown And Mouse.MovingObj Is Me Then

                Dim y As Integer
                Try
                    y = Int((e.Y - (mPosY + ParentWin.mPosY)) / CSng(mHeight / mMaxVal))
                Catch
                    y = 0
                End Try

                If y < 0 Then y = 0
                If y >= mMaxVal Then y = mMaxVal - 1

                If y <> mVal Then
                    mVal = y
                    CalcButtonRect()
                    RaiseEvent Changed()
                End If


            End If

        End Sub

        Public Sub SetValue(val As Integer)
            If val >= mMaxVal Then val = mMaxVal - 1
            If val < 0 Then val = 0
            mVal = val
            CalcButtonRect()
            RaiseEvent Changed()
        End Sub

        Public Sub SetPos(ByRef x As Integer, ByRef y As Integer) Implements Windows.mWindows.GUIObj.SetPos
            mPosX = x
            mPosY = y
            CalcClientRect()
        End Sub

        Public Sub SetSize(ByRef w As Integer, ByRef h As Integer)
            mWidth = w
            mHeight = h
            CalcClientRect()
        End Sub

        Public Event TabSwitch() Implements Windows.mWindows.GUIObj.TabSwitch

        Public Sub UnFocus() Implements Windows.mWindows.GUIObj.UnFocus

        End Sub

        Public ReadOnly Property ParentWin() As MirWindow Implements Windows.mWindows.GUIObj.ParentWin
            Get
                Return mWnd
            End Get
        End Property

        Public ReadOnly Property Name() As String Implements Windows.mWindows.GUIObj.Name
            Get
                Return mName
            End Get
        End Property

        Public Sub ReloadSurafces() Implements Windows.mWindows.GUIObj.ReloadSurafces

        End Sub
    End Class

End Namespace